<?php

class AuthController extends Zend_Controller_Action
{
	public function indexAction()
	{
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		if(!$data){
			$this->_redirect('auth/login');
		}
		$this->view->username = $data->username;
		$this->view->privilege = $data->id_typecompte;
		
		$auth = Zend_Auth::getInstance();
		
			 if (!$auth->hasIdentity()) 
			 {
			    $this->_redirect('/');
			 } else { 	
			    $userdata = $auth->getIdentity();
			    $this->username = $userdata->username;
			    $this->user_role = $userdata->id_typecompte;
	
			    $acl = Zend_Registry::get('acl');
				
				if($acl->isAllowed($this->user_role, 'addArticle'))
				{
				    $this->view->permit = 1;
				}
			 }
	}

	public function loginAction()
	{
		$users = new Application_Model_DbTable_Users();
		$form = new Application_Form_LoginForm();
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			if($form->isValid($_POST)){
					
				
				$data = $form->getValues();

				$auth = Zend_Auth::getInstance();
				$authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(),'users');
				$authAdapter->setIdentityColumn('username')
				->setCredentialColumn('password')
				->setCredentialTreatment('sha1(?)')
				->setIdentity($data['username'])
				->setCredential($data['password']);
					
				$result = $auth->authenticate($authAdapter);

				if($result->isValid()){
					$storage = new Zend_Auth_Storage_Session();
					$storage->write($authAdapter->getResultRowObject());
					$this->_redirect('auth/index');
				} else {
					$this->view->errorMessage = "Invalid username or password. Please try again.";
				}
			}
		}
	}
	
	public function signupAction()
	{
		$users = new Application_Model_DbTable_Users();
		$form = new Application_Form_RegistrationForm();
		$this->view->form=$form;
		if($this->getRequest()->isPost()){
			if($form->isValid($_POST)){
				$data = $form->getValues();
				if($data['password'] != $data['confirmPassword']){
					$this->view->errorMessage = "Password and confirm password don't match.";
					return;
				}
				if($users->checkUnique($data['username'])){
					$this->view->errorMessage = "Name already taken. Please choose      another one.";
					return;
				}
				unset($data['confirmPassword']);

				// crypte le MDP en md5 ( je pense que sh1 est mieux )
					
				$passwordhash=$data['password'];
				$password = sha1($passwordhash);
				$data['password']=$password;



				$users->insert($data);
				$this->_redirect('auth/login');
			}
		}
	}
	
	public function editAction()
	{
		$auth = Zend_Auth::getInstance();
		
		if($auth->hasIdentity()){
			$form = new Application_Form_RegistrationForm();
			$this->view->form = $form;
				
			if ($this->_request->isPost()) {
				$formData = $this->_request->getPost();
				if ($form->isValid($formData)) {
					$edit = new Application_Model_DbTable_Users();
					$username = $form->getValue('username');
					$firstname = $form->getValue('firstname');
					$lastname = $form->getValue('lastname');
					$email = $form->getValue('email');
					$id = $auth->getIdentity()->id;
					
					$data = $form->getValues();
					if($data['password'] != $data['confirmPassword']){
						$this->view->errorMessage = "Password and confirm password don't match.";
						return;
					}
					unset($data['confirmPassword']);
					
					// crypte le MDP en md5 ( je pense que sh1 est mieux )
						
					$passwordhash=$data['password'];
					$password = sha1($passwordhash);
					$edit->updateUser($id,$username, $firstname, $lastname, $email,$password);
					$this->_helper->redirector('index','auth');
				}else {
					$form->populate($formData);
	
				}
	
			}else {
				
					$user = new Application_Model_DbTable_Users();
					$row = $user->fetchRow('id=' .$auth->getIdentity()->id);
					$form->populate($row->toArray());
				
			}
			
			}
			else{
			$this->_redirect('auth/index');
		}
	}

	public function logoutAction()
	{
		$storage = new Zend_Auth_Storage_Session();
		$storage->clear();
		$this->_redirect('auth/login');
	}
}




